# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=libgdiplus
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=6.1
pkgrel=2
pkgdesc="An Open Source Implementation of the GDI+ API (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('MPL' 'LGPL')
url="https://www.mono-project.com/"
depends=("${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-libexif"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
source=("${_realname}-${pkgver}.tar.gz"::"https://download.mono-project.com/sources/libgdiplus/${_realname}-${pkgver}.tar.gz"
        "0001-libgdiplus-5.4-fix-shared-installation.patch"
        "0002-libgdiplus-5.4-fix-conflicting-types-error.patch"
        "0003-libgdiplus-5.4-fix-font-windows-error.patch"
        "0004-libgdiplus-5.4-disable-tests.patch"
        "0005-libgdiplus-fix-general-windows-error.patch")
sha256sums=('97d5a83d6d6d8f96c27fb7626f4ae11d3b38bc88a1726b4466aeb91451f3255b'
            'afae5618184a01373cb7d36358f64c40d45b70bddd0a1c35b43a32d15eb536b3'
            '3a3fd7a8ba6e0d6f1059ad1d595fb6c337066a2017296a6a11c8bd4aee78dc0b'
            '583252126424e2b7304766ecda77a5c10c6abf599eb6037552f25ffe73558ba8'
            '5a4fab187b389d7cfdc54ecc5443169f561655a62980b4d7ce2b2a58fea053a7'
            '1850fd2eaa5ebfba64a48cdd2cca1f7f46b0a14f2be0b1398195ea61ac31b9aa')

prepare() {
  cd ${_realname}-${pkgver}
  patch -Np1 -i "${srcdir}/0001-libgdiplus-5.4-fix-shared-installation.patch"
  patch -Np1 -i "${srcdir}/0002-libgdiplus-5.4-fix-conflicting-types-error.patch"
  patch -Np1 -i "${srcdir}/0003-libgdiplus-5.4-fix-font-windows-error.patch"
  patch -Np1 -i "${srcdir}/0004-libgdiplus-5.4-disable-tests.patch"
  patch -Np1 -i "${srcdir}/0005-libgdiplus-fix-general-windows-error.patch"
  autoreconf -fiv
}

build() {
  cd "${srcdir}"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-static \
    --enable-shared \
    --without-x11

  make
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make install DESTDIR="${pkgdir}"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix .pc file
  sed -i "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
  ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/libgdiplus.pc
}
